<template>
  <div class="navbar-container" v-if="!whiteList.includes($route.path)">
    <nav class="navbar">
      <Logo />
      <div class="navbar-links">
        <div
          v-for="item in navList"
          :key="item.path"
          class="nav-item"
          :class="{ active: $route.path === item.path }"
          @click="$router.push(item.path)"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="navbar-actions">
        <template v-if="isLoggedIn">
          <div class="user-profile-container">
            <div class="user-avatar" @click.stop="showUserCard = !showUserCard">
              <img src="@/assets/image/user.svg" alt="用户头像" />
            </div>
            <UserInfoCard
              ref="userInfoCardRef"
              class="user-info-dropdown"
              :class="{ show: showUserCard }"
            />
          </div>
        </template>
        <template v-else>
          <NuxtLink to="/login" class="login-btn">登录</NuxtLink>
          <NuxtLink to="/register" class="register-btn">注册</NuxtLink>
        </template>
      </div>
    </nav>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, computed } from "vue";
import { useRouter, useRoute } from "vue-router";

const router = useRouter();
const route = useRoute();
const { isLoggedIn, userInfo, name } = toRefs(useUserStore());
const showUserCard = ref(false);
const userInfoCardRef = ref<InstanceType<typeof UserInfoCard>>();

const navList = [
  {
    name: "SKU生成器",
    path: "/",
  },
  {
    name: "商品标题生成器",
    path: "/",
  },
  {
    name: "反馈建议",
    path: "/feedback",
  },
];

const whiteList = ["/login", "/register"];

// 点击显示/隐藏卡片
const handleClickOutside = (event: MouseEvent) => {
  const cardDom = userInfoCardRef.value?.$el;
  const clickDom = event.target;
  if (!cardDom) return;
  if (!cardDom.contains(clickDom)) showUserCard.value = false;
};

onMounted(() => document.addEventListener("click", handleClickOutside));
onUnmounted(() => document.removeEventListener("click", handleClickOutside));
</script>

<style scoped>
.navbar-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  padding: 1rem 1.5rem;
  z-index: 50;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.navbar {
  max-width: 1200px;
  margin: 0 auto;
  height: 3.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 1rem;
  padding: 0 1.5rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.1);
}

.navbar-logo {
  cursor: pointer;
  transition: transform 0.2s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.navbar-logo:hover {
  transform: scale(1.05);
}

.logo-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-text {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--primary-purple, #6b46c1);
}

.navbar-links {
  display: flex;
  gap: 1.5rem;
}

.nav-item {
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text-secondary, #4a5568);
  padding: 0.5rem 0.75rem;
  border-radius: 0.75rem;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
}

.nav-item:hover {
  color: var(--primary-purple, #6b46c1);
  background-color: rgba(107, 70, 193, 0.08);
}

.nav-item.active {
  color: var(--primary-purple, #6b46c1);
  background-color: rgba(107, 70, 193, 0.12);
  font-weight: 600;
}

.navbar-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.login-btn,
.register-btn {
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.5rem 1.25rem;
  border-radius: 9999px;
  transition: all 0.2s ease;
}

.login-btn {
  color: var(--primary-purple, #6b46c1);
  background-color: transparent;
  border: 1px solid var(--primary-purple, #6b46c1);
}

.login-btn:hover {
  background-color: rgba(107, 70, 193, 0.08);
  transform: translateY(-2px);
}

.register-btn {
  color: white;
  background-color: var(--primary-purple, #6b46c1);
}

.register-btn:hover {
  background-color: var(--secondary-purple, #805ad5);
  transform: translateY(-2px);
  box-shadow: 0 4px 6px rgba(107, 70, 193, 0.2);
}

.user-profile-container {
  position: relative;
}

.user-avatar {
  width: 2.75rem;
  height: 2.75rem;
  background-color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: 1px solid #e5e7eb;
  transition: all 0.2s ease;
}

.user-avatar:hover {
  border-color: var(--primary-purple, #6b46c1);
  transform: translateY(-2px);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.user-avatar img {
  width: 1.5rem;
  height: 1.5rem;
}

.user-info-dropdown {
  position: absolute;
  top: 3.25rem;
  right: 0;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-10px);
  transition: all 0.3s ease;
}

.user-info-dropdown.show {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

/* 平板和移动端的响应式调整 */
@media (max-width: 768px) {
  .navbar {
    padding: 0 1rem;
  }

  .navbar-links {
    gap: 0.75rem;
  }

  .nav-item {
    padding: 0.5rem;
    font-size: 0.85rem;
  }

  .login-btn,
  .register-btn {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
  }
}

@media (max-width: 640px) {
  .navbar-links {
    display: none;
  }
}
</style>
